#!/usr/bin/env bash
#
# Fix an invalid committer/author all commits of your repository.
#
# Usage:
# git-change-author <old-email> <new-name> <new-email>
#
# Author: Lars Schneider, https://github.com/larsxschneider
#

filter=$(cat <<EOF
    OLD_EMAIL='$1'
    NEW_NAME='$2'
    NEW_EMAIL='$3'

    if [ "\$GIT_COMMITTER_EMAIL" = "\$OLD_EMAIL" ]
    then
        export GIT_COMMITTER_NAME="\$NEW_NAME"
        export GIT_COMMITTER_EMAIL="\$NEW_EMAIL"
    fi
    if [ "\$GIT_AUTHOR_EMAIL" = "\$OLD_EMAIL" ]
    then
        export GIT_AUTHOR_NAME="\$NEW_NAME"
        export GIT_AUTHOR_EMAIL="\$NEW_EMAIL"
    fi
EOF
)

git filter-branch --env-filter "$filter" --tag-name-filter cat -- --all
